const successStatus = [200, 500]
export default {
	common: {
		baseUrl: "http://139.199.173.67",
		data: {},
		header: {
			"content-Type": "application/json",
		},
		method: "POST",
		dataType: "json",
		isToast: false,
		isNotLoading: false,
		isNotloginRedirect: false,
	},
	request(options = {}) {
		options.url = this.common.baseUrl + options.url;
		options.data = options.data || this.common.data;
		if (
			!options.header ||
			options.header == null ||
			options.header.token == "" ||
			!options.header.token
		) {
			let token = uni.getStorageSync("token") || "";
			options.header = {
				"content-Type": "application/json",
				token: token,
			};
		} else {
			options.header = options.header;
		}
		options.method = options.method || this.common.method;
		options.dataType = options.dataType || this.common.dataType;
		this.common.isToast = options.isToast || false;
		this.common.isNotLoading = options.isNotLoading || false;
		this.common.isNotloginRedirect = options.isNotloginRedirect || false;

		if (!this.common.isNotLoading) {
			uni.showLoading({
				title: "加载中",
			});
		}
		uni.showNavigationBarLoading();
		console.log(options, 44);
		return new Promise((res, rej) => {
			uni.request({
				...options,
				success: (result) => {
					uni.hideLoading();
					uni.hideNavigationBarLoading();
					if (!successStatus.includes(result.data.status)) {
						uni.showToast({
							title: result.data.message,
							icon: "none",
						});
						rej(result.data.msg, result.data.code);
						return;
					}

					let data = result.data.data;
					if (this.common.isToast) {
						uni.showToast({
							title: result.data.message,
							icon: "none",
							duration: 1100,
						});

					}
					res(data);
					if (result.data.status === 500) {
						uni.showToast({
							title: result.data.message,
							icon: "none",
						});
					}
				},
				fail(err) {
					uni.hideNavigationBarLoading();
					uni.hideLoading();
					uni.showToast({
						title: err.message || err,
						icon: "error",
						duration: 850,
					});
					rej(err);
				},
				complete: () => {
					uni.hideNavigationBarLoading();
					this.common.isToast = false;
					this.common.isNotloginRedirect = false;
					this.common.isNotLoading = false;
				},
			});
		});
	},
};
